package com.zztl.infiniteBand.presenter

import android.content.Context
import android.text.TextUtils
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.model.CheckAddressBean
import com.zztl.infiniteBand.model.GetVerifyBean
import com.zztl.infiniteBand.model.PlatformBean
import com.zztl.infiniteBand.model.TransferBean
import com.zztl.infiniteBand.net.FilterSubscriber
import com.zztl.infiniteBand.net.FilterSubscriber2
import com.zztl.infiniteBand.net.RetorfitServiceManger
import com.zztl.infiniteBand.net.converter.DataConverter
import com.zztl.infiniteBand.net.requestBean.CheckPlatformAddressRequest
import com.zztl.infiniteBand.net.requestBean.GetPlatformRequest
import com.zztl.infiniteBand.net.requestBean.GetVerifyRequest
import com.zztl.infiniteBand.net.requestBean.PayoutRequest
import com.zztl.infiniteBand.ui.view.mvpview.PayoutView
import com.zztl.infiniteBand.util.UiUtils
import com.zztl.infiniteBand.util.timer.ITimerListener
import com.zztl.infiniteBand.util.toast


/**
 * @desc
 * @auth ${user
 * @time 2018/6/13  15:53
 */
class PayoutPresenter : BaseCodePresenter<PayoutView>(), ITimerListener {


    fun getPlatformInfo(name: String, context: Context) {
        addSubscribe(RetorfitServiceManger
                .observe(RetorfitServiceManger.instance!!.apiService
                .getPlatform(DataConverter.instance.toJson(GetPlatformRequest(name))))
                .subscribe(object : FilterSubscriber<PlatformBean>(context) {
                    override fun success(platformBean: PlatformBean) {
                        val payoutView = view!! as PayoutView
                        payoutView.getPaltformSuccess(platformBean)
                    }


                }))
    }

    fun payout(request: PayoutRequest, context: Context) {
        addSubscribe(RetorfitServiceManger
                .observe(RetorfitServiceManger.instance!!.apiService
                .payout(DataConverter.instance.toJson(request)))
                .subscribe(object : FilterSubscriber<TransferBean>(context) {
                    override fun success(transferBean: TransferBean) {
                        val payoutView = view!! as PayoutView
                        payoutView.payoutSuccess(transferBean)
                    }


                }))
    }

    override fun getCode(getVerifyRequest: GetVerifyRequest, context: Context) {
        addSubscribe(RetorfitServiceManger
                .observe(RetorfitServiceManger.instance!!.apiService
                .getCode(DataConverter.instance.toJson(getVerifyRequest)))
                .subscribe(object : FilterSubscriber2<GetVerifyBean>(context) {
                    override fun success(bean: GetVerifyBean) {
                        view!!.getCodeSuccess()
                    }

                    override fun fail(message: String?) {

                            view!!.getCodeFail(message!!)

                    }

                }))

    }


    fun checkAddress(address: String, context: Context) {
        val request = CheckPlatformAddressRequest(address)
        addSubscribe(RetorfitServiceManger
                .observe(RetorfitServiceManger.instance!!.apiService
                        .checkPlatformAddress(DataConverter.instance.toJson(request)))
                .subscribe(object : FilterSubscriber2<CheckAddressBean>(context) {

                    override fun success(bean: CheckAddressBean) {
                        val payoutView = view!! as PayoutView
                        payoutView.checkSuccess(bean)
                    }

                    override fun fail(msg: String?) {
                        view!!.getCodeFail()
                    }
                }))
    }

    fun check(address: String, money: String, code: String, pwd: String): Boolean {
        if (TextUtils.isEmpty(address)) {
//            ToastUtils.show(UiUtils.context, UiUtils.getString(R.string.地址为空))
            UiUtils.context.toast(UiUtils.getString(R.string.地址为空))
            return false
        }
        if (TextUtils.isEmpty(money)) {
            UiUtils.context.toast(UiUtils.getString(R.string.转出金额为空))
            return false
        }
        if (TextUtils.isEmpty(pwd)) {
            UiUtils.context.toast(UiUtils.getString(R.string.交易密码不能为空))
            return false
        }
        if (TextUtils.isEmpty(code)) {
            UiUtils.context.toast(UiUtils.getString(R.string.验证码为空))
            return false
        }

        try {
            if (java.lang.Double.valueOf(money) <= 0) {

                UiUtils.context.toast(UiUtils.getString(R.string.转出金额不能为0))
                return false
            }
        } catch (e: Exception) {
            UiUtils.context.toast(UiUtils.getString(R.string.请输入正确的转账金额))
            return false
        }


        return true
    }

}
